<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Map\Multimap;

use LogicException;
use Skyline\Structure\Map\HashMap;

/**
 * Abstract implementation of MultimapInterface that uses a HashMap and
 * collection-backed entries
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractMultimap extends HashMap implements MultimapInterface
{
	/**
	 * Returns true if a given value exists in this map
	 * 
	 * @param mixed $value
	 * @return bool
	 */
	public function containsValue($value)
	{
		foreach($this->entries as $entry) {
			if($entry->getValue()->contains($value)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns a set of the values in this map
	 * @return SetInterface<mixed>
	 */
	public function getValues()
	{
		$set = $this->createSet();
		foreach($this->entries as $entry) {
			$set->addAll($entry->getValue()->toArray());
		}
		return $set;
	}
	
	/**
	 * Returns the first key associated with a given value (if any)
	 * 
	 * @param mixed $value
	 * @return mixed|null Returns null if no key is associated with the value
	 */
	public function keyOf($value)
	{
		foreach($this->entries as $entry) {
			if($entry->getValue()->contains($value)) {
				return $entry->getKey();
			}
		}
	}
	
	/**
	 * Returns all keys associated with a given value
	 * 
	 * @param mixed $value
	 * @return mixed[]
	 */
	public function keysOf($value)
	{
		$keys = array();
		foreach($this->entries as $entry) {
			if($entry->getValue()->contains($value)) {
				$keys[] = $entry->getKey();
			}
		}
		return $keys;
	}
	
	/**
	 * Removes a value from the map
	 * 
	 * @param mixed $value
	 * @return void
	 */
	public function removeValue($value)
	{
		foreach($this->entries as $hash => $entry) {
			$collection = $entry->getValue();
			$collection->remove($value);
			if(!$collection->count()) {
				// Remove emptied entries from the map
				unset($this->entries[$hash]);
			}
		}
	}
	
	/**
	 * Returns this map in the form of an associative array (if possible)
	 * 
	 * @throws LogicException If operation is unsupported
	 * @return mixed[mixed]
	 */
	public function toArray()
	{
		$this->sortEntries();
		$entries = array();
		foreach($this->entries as $entry) {
			$key = $entry->getKey();
			$value = $entry->getValue();
			if(is_object($key)) {
				throw new LogicException('Operation not supported; object keys exist');
			}
			$entries[$key] = $value->toArray();
		}
		return $entries;
	}
}